#include <iostream>
struct Student {
    char num[10];
    char name[8];
    int score[3];
    float aver;
} st[10], s;

int main() {
    FILE *fp, *fp1;
    int i, j, t, n;
    printf("\nNO.:");
    scanf("%s", s.num);
    printf("name:");
    scanf("%s", s.name);
    printf("score1, score2, score3:");
    scanf("%d %d %d", s.score[0], s.score[1], s.score[2]);
    s.aver = (s.score[0] + s.score[1] + s.score[2]) / 3.0;

    if ((fp = fopen("D:\\C\\stud_sort.txt", "r")) == NULL) {
        printf("cannot open file!!\n");
        exit(0);
    }
    printf("original data: \n");
    for (i = 0; fread(&st[i], sizeof(struct Student), 1, fp) != 0; i++) {
        printf("\n%8s%8s", st[i].num, st[i].name);
        for (j = 0; j < 3; j++) {
            printf("%8d", st[i].score[j]);
        }
        printf("%10.2f", st[i].aver);
    }

    n = i;
    for (t = 0; st[t].aver > s.aver && t < n; t++)
    printf("\nNow:\n");
    fp1 = fopen("D:\\C\\sort1.txt", "w");
    for (i = 0; i < t; i++) {
        fwrite(&st[i], sizeof(struct Student), 1, fp1);
        printf("\n%8s%8s", st[i].num, st[i].name);
        for (j = 0; j < 3; j++) {
            printf("%8d", st[i].score[j]);
        }
        printf("%10.2f", st[i].aver);
    }
    fwrite(&s, sizeof(struct Student), 1, fp1);
    printf("\n %8s %7s %7d %7d %7d%10.2f", s.num, s.name, s.score[0],
            s.score[1], s.score[2], s.aver);

    for (i = t; i < n; i++) {
        fwrite(&st[i], sizeof(struct Student), 1, fp1);
        printf("\n%8s%8s", st[i].num, st[i].name);
        for (j = 0; j < 3; j++) {
            printf("%8d", st[i].score[j]);
        }
        printf("%10.2f", st[i].aver);
    }
    printf("\n");
    fclose(fp1);
    fclose(fp);
    return 0;
}
